package org.example.messenger.hibernate.dao;

import java.lang.reflect.ParameterizedType;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class GenericDAO<T> extends HibernateDaoSupport implements
		IGenericDAO<T> {

	protected Class<T> persistentClass = figureOutPersistentClass();

	@Override
	public Long create(T objectToCreate) {
		return (Long) getSession().save(objectToCreate);
	}

	@Override
	public T get(Long id) {
		return (T) getSession().get(persistentClass, id);
	}

	@Override
	public void update(T object) {
		create(object);

	}

	@Override
	public void delete(T object) {
		getSession().delete(object);

	}

	protected Class<T> figureOutPersistentClass() {
		Class<T> clazz = (Class<T>) ((ParameterizedType) (getClass()
				.getGenericSuperclass())).getActualTypeArguments()[0];
		return clazz;
	}
}
